package com.hd.core.project.module.controller;

import java.util.Arrays;

import java.util.List;
import com.hd.core.project.module.base.WrapperBaseController;
import com.hd.core.project.module.config.StudentModuleProperties;
import com.hd.core.project.module.domain.Select2;
import com.hd.core.project.module.domain.Student;
import com.hd.core.project.module.domain.Classes;
import com.hd.core.project.module.service.ClassesService;
import com.hd.core.project.module.service.StudentService;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.core.framework.aspectj.lang.annotation.Log;
import com.hd.core.framework.aspectj.lang.constant.BusinessType;
import com.hd.core.framework.web.page.TableDataInfo;
import com.hd.core.framework.web.domain.AjaxResult;

/**
 * <p>学生管理-控制层</p>
 * <p>提供：基本CRUD操作和条件查询操作</p>
 * 
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年8月19日
 * @package com.hd.core.project.module.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("/module/student/student")
public class StudentController extends WrapperBaseController<StudentModuleProperties> {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ClassesService classesService;
	
	StudentController() {
		super("/student");
	}
	
	/**
	 * <p>跳转到显示视图</p>
	 * <p>访问权限: module-student:student:view</p>
	 * <p>路径: /templates/module/student/student/student.html</p>
	 * 
	 * @return 显示视图
	 */
	@GetMapping
	@RequiresPermissions("module-student:student:view")
	public String studentList(ModelMap mmap){
		List<Classes> classes = classesService.selectList(null);
		mmap.put("classes",classes);
	    return getResponseViewPath("/student");
	}
	
	/**
	 * <p>查询学生<p/>
	 * <p>根据Entity实体内的成员属性的值来当做查询条件<p/>
	 * <p>访问权限: module-student:student:list</p>
	 * 
	 * @param entity 条件实体
	 * @return 符合条件的学生数据
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("module-student:student:list")
	public TableDataInfo list(Student entity){
		startPage();
		List<Student> result = studentService.selectList(entity);
		return getDataTable(result);
	}
	
	/**
	 * <p>跳转到新增学生的视图</p>
	 * <p>路径: /templates/module/student/student/add.html</p>
	 * 
	 * @return 新增学生功能视图
	 */
	@GetMapping("/add")
	public String add(){
	    return getResponseViewPath("/add");
	}
	
	/**
	 * <p>新增一名学生<p/>
	 * <p>访问权限: module-student:student:add</p>
	 * 
	 * @param entity 数据实体
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/add")
	@Log(title = "学生管理", action = BusinessType.INSERT)
	@RequiresPermissions("module-student:student:add")
	public AjaxResult addSave(Student entity){
		return toAjax(studentService.insert(entity));
	}

	/**
	 * <p>根据ID修改学生数据</p>
	 * <p>根据URL上的id值,先查询出要来,回现到页面上</p>
	 * <p>路径: /templates/module/student/student/edit.html</p>
	 * 
	 * @return 修改视图路径
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap){
		Student student = studentService.selectById(id);
		mmap.put("student", student);
	    return getResponseViewPath("/edit");
	}
	
	/**
	 * <p>修改并保存<p/>
	 * <p>根据实体的Id值来进行保存操作<p/>
	 * <p>访问权限: module-student:student:edit</p>
	 * 
	 * @param entity 参数实体也是判断条件
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("module-student:student:edit")
	@Log(title = "学生管理", action = BusinessType.UPDATE)
	public AjaxResult editSave(Student entity){		
		return toAjax(studentService.updateById(entity));
	}
	
	/**
	 * <p>根据id删除对应的数据,可以接收多个ID<p/>
	 * <p>访问权限: module-student:list:remove</p>
	 * 
	 * @param ids 需要被删除的文明宿舍所对应的id号
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping( "/remove")
	@RequiresPermissions("module-student:student:remove")
	@Log(title = "学生管理", action = BusinessType.DELETE)
	public AjaxResult remove(String[] ids){
		List<String> studentsId = Arrays.asList(ids);
		return toAjax(studentService.delete(studentsId));
	}
	
	/**
	 * <p>查询班级并以Select2的格式返回输出<p/>
	 * <p>访问权限: module-student:list:add,module-student:student:edit</p>
	 * 
	 * @return http状态消息
	 */
	@ResponseBody
	@GetMapping("/queryClassFormatSelect2")
	@RequiresPermissions(
		value = {"module-student:student:add","module-student:student:edit"},
		logical = Logical.OR
	)
	public AjaxResult queryClassFormatSelect2(){
		List<Select2> select2Result = classesService.selectListFmtSelect2();
		return toSelect2Ajax(select2Result);
	}
}
